#include <iostream>
#include <string.h>
#define lowbit(x) ((x) & (-(x)))
#define int long long
using namespace std;
const int inf = 0x7ffffffffffffff;
int n;
int a[200005];
int mx[1000005];
int v[200005];
inline int f(int l, int r) { return (a[l] == a[r] ? a[l] : 0); }
int dp[200005], pre[200005];
struct PBIT {
    int bit[1000005];
    void add(int x, int y) { for (++x; x <= 1000000; x += lowbit(x)) bit[x] = max(bit[x], y); }
    int query(int x) {
        int ret = -inf;
        for (++x; x; x -= lowbit(x)) ret = max(ret, bit[x]);
        return ret;
    }
} pbit;
struct SBIT {
    int bit[1000005];
    void add(int x, int y) { for (++x; x; x -= lowbit(x)) bit[x] = max(bit[x], y); }
    int query(int x) {
        int ret = -inf;
        for (++x; x <= 1000000; x += lowbit(x)) ret = max(ret, bit[x]);
        return ret;
    }
} sbit;
signed main() {
    freopen("color2.in", "r", stdin);
    freopen("color.out", "w", stdout);
    int tc;
    cin >> tc;
    while (tc--) {
        for (int i = 0; i <= 1000004; i++) mx[i] = pbit.bit[i] = sbit.bit[i] = -inf;
        memset(a, 0, sizeof a);
        memset(pre, 0, sizeof pre);
        cin >> n;
        for (int i = 1; i <= n; i++) cin >> a[i];
        for (int i = 1; i < n; i++) v[i] = f(i, i + 1), pre[i] = pre[i - 1] + v[i];
        for (int i = 1; i <= n; i++) {
            dp[i] = pre[i - 1];
            if (i != n) {
                dp[i] = max(dp[i], mx[a[i + 1]] + pre[i - 1] + a[i + 1]);
                dp[i] = max(dp[i], pre[i - 1] + max(pbit.query(a[i + 1] - 1), sbit.query(a[i + 1] + 1)));
            } else 
                dp[i] = max(dp[i], pbit.query(1000000) + pre[i - 1]);
            mx[a[i]] = max(mx[a[i]], dp[i] - pre[i]);
            pbit.add(a[i], mx[a[i]]);
            sbit.add(a[i], mx[a[i]]);
            // for (int j = 0; j < i; j++) {
                // dp[i] = max(dp[i], dp[j] + pre[i - 1] - pre[j] + f(j, i + 1));
            // }
        }
        cout << dp[n] << "\n";
        // cout << "asdf\n";
    }
    return 0;
}